package com.web.Test0;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class SocketRecive {
    public static void main(String[] args) throws Exception {
        //1.创建Socket对象
        DatagramSocket socket = new DatagramSocket(10085);
        //2.创建数据包
        byte[] bytes=new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        //3.调用socket方法接受数据
        socket.receive(packet);
        //4.解析数据包
        String str=new String(packet.getData(),0,packet.getLength());
        InetAddress address = packet.getAddress();
        String ip = address.getHostAddress();
        System.out.println(ip+"-------+-"+str);
        //5.释放数据
        socket.close();
    }
}
